package com.dreamlin.basepro.common;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Parcelable;
import android.text.TextUtils;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.util.regex.Pattern;

public class Utils {

    private static final String REGEX_MOBILE = "^((13[0-9])|(14[5-9])|(15[^4])|(16[5-7])|(17[0-8])|(18[0-9])|(19[1,8-9])|)\\d{8}$";

    public static boolean isPhoneNum(String number) {
        return Pattern.matches(REGEX_MOBILE, number);
    }

    public static void callPhone(Activity activity, String phone) {
        if (ContextCompat.checkSelfPermission(activity, Manifest.permission.CALL_PHONE) !=
                PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.CALL_PHONE},
                    0xAA);
            return;
        }
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(activity, "电话号码为空，无法拨打!", Toast.LENGTH_SHORT).show();
            return;
        }
        phone = phone.trim();
        phone = phone.replace("-", "");
        if (!isPhoneNum(phone)) {
            Toast.makeText(activity, "电话号码格式有误，请查证后再拨!", Toast.LENGTH_SHORT).show();
            return;
        }
        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri callData = Uri.parse("tel:".concat(phone));
        intent.setData(callData);
        activity.startActivity(intent);
    }

    public static final String MODEL = "MODEL";
    public static final String TITLE = "TITLE";
    public static final String FLAG = "FLAG";

    public static void jump(Activity compatActivity, Class<? extends AppCompatActivity> jumpTo, String title) {
        Intent intent = new Intent(compatActivity, jumpTo);
        if (!TextUtils.isEmpty(title))
            intent.putExtra(TITLE, title);
        compatActivity.startActivity(intent);
    }

    public static void jump(Activity compatActivity, Class<? extends AppCompatActivity> jumpTo) {
        jump(compatActivity, jumpTo, null);
    }

    public static void jumpWithFLag(Activity activity, Class<? extends AppCompatActivity> to, int flag, String title) {
        Intent intent = new Intent(activity, to);
        if (!TextUtils.isEmpty(title))
            intent.putExtra(TITLE, title);
        intent.putExtra("FLAG", flag);
        activity.startActivity(intent);
    }

    public static void jumpWithFLag(Activity activity, Class<? extends AppCompatActivity> to, int flag) {
        jumpWithFLag(activity, to, flag, null);
    }

    public static void jumpWithModel(Activity compatActivity, Class<? extends AppCompatActivity> jumpTo, Parcelable model) {
        jumpWithModel(compatActivity, jumpTo, model, null);
    }

    public static void jumpWithModel(Activity compatActivity, Class<? extends AppCompatActivity> jumpTo, Parcelable model, String title) {
        Intent intent = new Intent(compatActivity, jumpTo);
        if (!TextUtils.isEmpty(title))
            intent.putExtra(TITLE, title);
        intent.putExtra(MODEL, model);
        compatActivity.startActivity(intent);
    }


}
